package cn.pconline.officialclub.web.price;

import cn.pconline.autoclub.service.ClubService;
import cn.pconline.autoclub.util.DateUtils;
import cn.pconline.autoclub.util.Result;
import cn.pconline.autoclub.util.WebUtils;
import cn.pconline.officialclub.entity.OActivity;
import cn.pconline.officialclub.entity.OClub;
import cn.pconline.officialclub.service.OActivityService;
import cn.pconline.officialclub.service.OClubService;
import cn.pconline.officialclub.web.WebKit;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author xulin
 */
@RequestMapping(value = "/brand/price")
@Controller
public class PriceOClubController extends WebKit {

    @Autowired
    private OClubService oClubService;
    @Autowired
    private OActivityService oActivityService;
    @Autowired
    private ClubService clubService;

    /**
     * 根据车系ID查询品牌车友会(车型库页面接口)
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/1.0/listOfficialClubBySeriesId.do", method = RequestMethod.GET)
    public void listOfficialClubBySeriesId(HttpServletRequest request, HttpServletResponse response) {
        //设置缓存
        WebUtils.setCacheHeader(response, 3600);
        WebUtils.setEncoding(request, response);
        long seriesId = WebUtils.paramLong(request, "seriesId", 0);
        String callback = WebUtils.param(request, "callback", "");
        int xssEncode = WebUtils.paramInt(request, XSS_ENCODE, XSSENCODECODE);
        int loadActivity = WebUtils.paramInt(request, "loadActivity", 0);
        boolean isLoad  = loadActivity >0;
        boolean isXss = xssEncode == XSSENCODECODE;
        if (seriesId == 0) {
            printMsg(response, Result.dataError("缺少车系ID"));
            return;
        }
        OClub oClub = oClubService.queryClubBySeriesId(seriesId);
        JSONObject obj = new JSONObject();
        if (oClub != null) {
            obj = oClub.getJSON(isXss);
            obj.put("url", systemConfig.getBrandClubRoot()+"/"+oClub.getUrl());
            if(isLoad){
                OActivity oActivity = oActivityService.getLatestActivity(oClub.getoClubId());
                if (oActivity != null && oActivity.getEndDate().after(DateUtils.getNow())) {
                    obj.put("activity",putActivityUrl(oClub,oActivity,oActivity.getJSON(isXss)) );
                }
            }
        }
        printMsg(response, Result.success(obj), callback);
    }
    private JSONObject putActivityUrl(OClub oClub,OActivity oActivity,JSONObject o){
         if (o == null) {
            return null;
         }
         o.put("url", systemConfig.getBrandClubRoot()+"/"+oClub.getUrl()+"/activity/a"+oActivity.getActivityId()+"/");
         o.put("statusCode", oActivityService.getActivityStatusDesc(0, oActivity).getString("activityCode"));
         o.put("statusMsg", oActivityService.getActivityStatusDesc(0, oActivity).getString("activityMsg"));
         return o;
    }
}
